package com.heima.kafka.sample;

/**
 * @Author yuan
 * @Date 2023/1/30
 * @ApiNote
 */

import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * 生产者
 */
public class ProducerStreamQuickStart {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 1. kafka链接配置信息
        Properties prop = new Properties();
        prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        prop.put(ProducerConfig.ACKS_CONFIG, "all");
        prop.put(ProducerConfig.RETRIES_CONFIG, 10);
        prop.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");

        // 2. 创建生产者
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(prop);

        // 3. 发送消息 流式处理
        for (int i = 0; i < 5; i++) {
            ProducerRecord<String, String> kvProducerRecord = new ProducerRecord<String, String>("itcast-topic-input", "hello kafka");
            producer.send(kvProducerRecord);
        }

        // 4. 关闭消息通道(必须关闭,否则信息发送不成功)
        producer.close();

    }
}
